
package com.contractmodule.servlet;
import com.contractmodule.bean.TbDispatchListProjectBackSectionMiddle;
import com.contractmodule.dao.*;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbDispatchListProjectBackSectionMiddleServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers) request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        String dispatchListCode=request.getParameter("dispatchListCode");
        int projectStateId=Integer.parseInt(request.getParameter("projectStateId"));
            if(new TbDispatchListProjectBackSectionMiddleDAO().add(new TbDispatchListProjectBackSectionMiddle(dispatchListCode,projectStateId,userId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_DispatchListProjectBackSectionMiddle", "DispatchListProjectBackSectionMiddle", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int dispatchListProjectBackSectionMiddle=Integer.parseInt(request.getParameter("dispatchListProjectBackSectionMiddle"));
        String dispatchListCode=request.getParameter("dispatchListCode");
        int projectStateId=Integer.parseInt(request.getParameter("projectStateId"));
            if(new TbDispatchListProjectBackSectionMiddleDAO().edit(new TbDispatchListProjectBackSectionMiddle(dispatchListProjectBackSectionMiddle,dispatchListCode,projectStateId,userId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
             request.getSession(true).setAttribute("tbDispatchListProjectBackSectionMiddle",new TbDispatchListProjectBackSectionMiddleDAO().getTbDispatchListProjectBackSectionMiddleByDispatchListProjectBackSectionMiddle(Integer.parseInt(request.getParameter("dispatchListProjectBackSectionMiddle"))));
            response.sendRedirect("ContractModule/TbDispatchListProjectBackSectionMiddleOperate.jsp");
    	}
        if("showadd".equals(method)){
            
            request.getSession(true).removeAttribute("tbDispatchListProjectBackSectionMiddle");
            response.sendRedirect("ContractModule/TbDispatchListProjectBackSectionMiddleOperate.jsp");
        }
        if("backSectionListByDispatchCode".equals(method)){//get the backSection by dispatchId
        	String dispatchCode = request.getParameter("dispatchCode");
        	String lastBackSectionId = new TbDispatchListProjectBackSectionMiddleDAO().getBackSectionId(dispatchCode);
        	request.getSession(true).removeAttribute("tbBackSectionList");
        	if(!lastBackSectionId.equals("")){
        		request.getSession(true).setAttribute("tbBackSectionList",new TbBackSectionDAO().getTbBackSectionAll(lastBackSectionId));
        	}
        	response.sendRedirect("ContractModule/TbBackSectionList.jsp");
        }
        if("projectStateListByDispatchCode".equals(method)){
        	String dispatchCode = request.getParameter("dispatchCode");
        	String lastProjectStateId = new TbDispatchListProjectBackSectionMiddleDAO().getProjectStateId(dispatchCode);
        	request.getSession(true).removeAttribute("tbProjectStateList");
        	if(!lastProjectStateId.equals("")){
        		request.getSession(true).setAttribute("tbProjectStateList",new TbProjectStateDAO().getTbProjectStateAll(lastProjectStateId));
        	}
            response.sendRedirect("ContractModule/TbProjectStateList.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbDispatchListProjectBackSectionMiddleList",new TbDispatchListProjectBackSectionMiddleDAO().getTbDispatchListProjectBackSectionMiddleAll());
        response.sendRedirect("ContractModule/TbDispatchListProjectBackSectionMiddleList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
